<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller 
{
	//登陆显示
    public function index()
    {
        $this->display();
    }

    //登陆
    public function login()
    {
        if (IS_POST) {
            $username = I('post.username');
            $password = I('post.pass');
            $password = md5($password);

            $userinfo = M('admins')->where(array('username'=>$username))->find();
            if (!$userinfo) {
                $arr = array(
                    "error" => 0,
                    "info" => "账号不存在"
                    );
                echo json_encode($arr);
                exit;
            }
            if ($userinfo['password']!=$password) {
                $arr = array(
                        "error" => 0,
                        "info" => "密码输入有误"
                    );
                echo json_encode($arr);
                exit;
            }
            if ($userinfo['is_dongjie'] !=1) {
                $arr = array(
                    "error" => 0,
                    "info" => "账号已停用"
                    );
                echo json_encode($arr);
                exit;
            }
            //成功登陆

            //更新登陆时间 ip
            $login_time= time();
            $login_ip = get_client_ip();
            $data = array('login_time'=>$login_time,'login_ip'=>$login_ip);
            M('admins')->where(array('id'=>$userinfo["id"]))->setField($data);

            //设置session登录成功
            $_SESSION["id"] = $userinfo["id"];
            $_SESSION["username"] = $userinfo["username"];

            $arr = array(
                "error" => 1,
                "info" => "登陆中,请稍等"
                );
            echo json_encode($arr);

        }
    }

    //退出
    public function logout()
    {
        session_unset();
        session_destroy();
        $this->redirect('admin/login/index');
    }

    
}